
#include <iostream>
#include <cstdlib> /* system(), srand(), rand() */
#include <string>
#include <vector>

#include "boggle.h"

using namespace std;

bool isNeighbour(const char *a, const char *b) {
   return true;
}

bool boggle::check_boggle_validity(string word) {
 
   const char *token = word.c_str();
   const char *ptr1 = token;
   const char *ptr2 = token + 1;

   while(*ptr2 != '\0') {
      if( isNeighbour(ptr1,ptr2) )
         continue;
       else
         return false;
   }
  return true;
}

void boggle::accept_userlist() {

   string word; /* user input */
   cin >> word;
 
   while( !word.empty() ) {
      
      if( word.compare("!") == 0 ) {
         break;
      }
 
      if( check_boggle_validity(word) ) {
         userlist.push_back(word);
      }
      cin >> word; 
   }
}

void boggle::print_userlist() {
    cout << endl << "You entered : " << endl;
    for(vector<string>::iterator it = userlist.begin(); it != userlist.end(); it++){
	cout << "\t" << *it;
    }
    cout << endl;
}

void boggle::print_boggle() {
   char *ptr_let = letters;
   char char_to_print;
   char vowels[] = {"aeiou"};
   int n_rows = 3;  /* number of rows */
   int j, k;        /* loop counters */
   int n;           /* for random number generation, loop counter, tmp variable*/
   int vowel_index; /* vowel index */
   int insert_index;
   int total_num_vowels;
   int letters_max;
   srand(time(0)); /* seed random number generator, using epoc time */

   /* randomly generating letters */
   for(j = 0; j < (n_rows * n_rows); j++) {
      n = rand() % 26;
      letters[j] = (char)(n + 65);
   }

   /* randomly swap (total number of letters)/3 with vowels */
   letters_max = n_rows * n_rows; // (sizeof letters) - 1;
   total_num_vowels = letters_max/3;
   for(j = 0; j < total_num_vowels; j++){
      insert_index = rand() % (letters_max - 1);   /* generate random letter index*/
      vowel_index = rand() % 5;                    /* generate random vowel index*/
      letters[insert_index] = vowels[vowel_index]; /* swap the letter with vowel */
   }

   /* begin console output */
   system("clear");
   cout << "\n Let's Boggle! \t\t\t\t\t To exit type '!'" << endl;
   cout << endl << "\t\t|";

   /* print first line */
   for (j = 0; j < n_rows; j++)
      cout << "---|";
   cout << endl;

   /* display letters in a formatted manner*/
   for(k = 0; k < n_rows; k++) {
      for (j = 0; j < n_rows; j++) {
         char_to_print = toupper( *(ptr_let + j) );
         if(j == 0) 
	   cout << "\t\t";
         if (char_to_print == 'Q')
            cout << "| " << char_to_print << "u";
         else
            cout << "| " << char_to_print << " ";
      }
      cout << "|" << endl;
      ptr_let += n_rows;
      
      /* line below a row of letters */
      cout << "\t\t|";
      for( n = 0; n < n_rows; n++)
        cout << "---|";
      cout << endl;
   }

   cout << endl << endl;
   return;
}

